import os

os.environ['KIVY_IMAGE'] = 'pil,sdl2'
import win32timezone
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
import threading
from kivy.config import Config

Config.set('kivy', 'default_font', [
    'msgothic',
    'fonts/msyh.ttc'])
from kivy.uix.popup import Popup
from app_service import RequestHD


class Check(BoxLayout):
    def click(self, obj):
        rhd = RequestHD(self.fileaddrs)
        obj.disabled = True
        t = threading.Thread(target=rhd.console_print, args=[self])
        t.start()

    def select(self, instance):
        content = SelectFile()
        self.popup = Popup(title="选择request请求配置文件(json格式)", content=content, size_hint=(0.9, 0.9))
        self.popup.open()


class SelectFile(BoxLayout):
    def confirm(self, instance):
        json_files = set()
        json_file_names = set()
        for file in instance.selection:
            file = str(file)
            file_name = file.split("\\")[-1]
            file_suffix = file_name.split(r'.')[-1]
            if (file_suffix == 'json'):
                json_files.add(file)
                json_file_names.add(file_name)
                # TODO:改为弹窗
                # self.parent.parent.parent.parent.children[1].ids['consoleId'].text = '%s 文件格式非json 格式，请选择json格式文件上传'% (file_name)
                # self.parent.parent.parent.dismiss()
                # break

        self.parent.parent.parent.parent.children[1].fileaddrs = json_files
        self.parent.parent.parent.parent.children[1].ids['fileaddr'].text = '选中配置为 [%s]' % (json_file_names)
        self.parent.parent.parent.parent.children[1].ids['run'].disabled = False
        self.parent.parent.parent.dismiss()


class BestApi(App):
    def build(self):
        return Check()


if __name__ == "__main__":
    BestApi().run()
